package cn.green.core.components.store.field;

import cn.green.core.components.exceptions.FieldUnitNullException;
import cn.green.core.components.field.FieldUnitTypeCom;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * @author wang.yishu
 * @version 1.0
 * @className FieldUnitTypeStore
 * @description 字段单位仓库
 * @date 2024/11/18 9:31
 */
public class FieldUnitTypeStore {

    private static final Map<String, FieldUnitTypeCom> FIELD_UNIT_TYPE_HASH_MAP = new HashMap<>();
    private static final JSONArray OPTIONS = new JSONArray();

    public static void put(FieldUnitTypeCom store) {
        FIELD_UNIT_TYPE_HASH_MAP.put(store.unitCode(), store);
        JSONObject option = new JSONObject();
        option.put("label", store.unitName());
        option.put("value", store.unitCode());
        OPTIONS.add(option);
    }

    public static FieldUnitTypeCom get(String typeCode) {
        if (!FIELD_UNIT_TYPE_HASH_MAP.containsKey(typeCode)) {
            throw new FieldUnitNullException(typeCode);
        }
        return FIELD_UNIT_TYPE_HASH_MAP.get(typeCode);
    }

    public static JSONArray options() {
        return OPTIONS;
    }

}
